/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
// USER INCLUDES
#include "ImageFadeControl.h"
#include "FadeProvider.h"

//////////////////////////////////////////////////////////////////////////
CImageFadeControl* CImageFadeControl::NewL( const TRect& aRect,
	CFadeProvider* aFader )
	{
	CImageFadeControl* self = CImageFadeControl::NewLC( aRect, aFader );
	CleanupStack::Pop( self );
	return self;
	}

//////////////////////////////////////////////////////////////////////////
CImageFadeControl* CImageFadeControl::NewLC( const TRect& aRect,
	CFadeProvider* aFader )
	{
	CImageFadeControl* self = new ( ELeave ) CImageFadeControl();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aFader );
	return self;
	}

//////////////////////////////////////////////////////////////////////////
CImageFadeControl::CImageFadeControl()
	{
	// No implementation required
	}

//////////////////////////////////////////////////////////////////////////
CImageFadeControl::~CImageFadeControl()
	{
	delete iDelayTimer;
	iFader->StopTransition();
	if ( iOwnsFader )
		{
		delete iFader;
		iFader = NULL;
		}
	}

//////////////////////////////////////////////////////////////////////////
void CImageFadeControl::ConstructL( const TRect& aRect, CFadeProvider* aFader )
	{
	CStaticImageControl::ConstructL( aRect );
	iOwnsFader = ( aFader == NULL );
	iFader = aFader ? aFader : CFadeProvider::NewL();
	iFader->StepCompleted
		+= MAKEMETHODEVENTHANDLER0(void, CImageFadeControl, HandleStepCompleted, this);
	iFader->TransitionCompleted
		+= MAKEMETHODEVENTHANDLER0(void, CImageFadeControl, HandleFadeCompleted, this);

	// Defaults to 2 seconds
	iDelay = 1000000;
	iDelayTimer = CPeriodic::New( CPeriodic::EPriorityLow );
	// Defaults to 1000 ms = 1s;
	iTransitionTime = 1000;
	}

//////////////////////////////////////////////////////////////////////////
void CImageFadeControl::SetImage( CFbsBitmap* aBitmap )
	{
	iSourceBitmap = aBitmap;
	iFader->SetupTransition( aBitmap, NULL, iTransitionTime );
	}

//////////////////////////////////////////////////////////////////////////
CFbsBitmap* CImageFadeControl::Image() const
	{
	return iSourceBitmap;
	}

//////////////////////////////////////////////////////////////////////////
void CImageFadeControl::StartFade( TInt aFadeType )
	{
	iFadeType = aFadeType;
	iFader->SetFadeDirection( aFadeType & EFadeIn );
	iFader->BeginTransition();
	}

//////////////////////////////////////////////////////////////////////////
void CImageFadeControl::HandleStepCompleted()
	{
	CStaticImageControl::SetImage( iFader->TransitionBitmap() );
	DrawNow();
	}

//////////////////////////////////////////////////////////////////////////
void CImageFadeControl::HandleFadeCompleted()
	{
	if ( ( iFadeType & EFadeIn ) && ( iFadeType & EFadeOut )
		&& iFader->FadeDirection() )
		{
		iDelayTimer->Start( iDelay, iDelay,
			TCallBack( DelayTimerCallback, this ) );
		}
	else
		{
		FadeCompleted();
		}
	}

//////////////////////////////////////////////////////////////////////////
TInt CImageFadeControl::DelayTimerCallback( TAny* aPtr )
	{
	CImageFadeControl* self = ( CImageFadeControl* ) aPtr;
	self->iDelayTimer->Cancel();
	self->iFader->SetFadeDirection( EFalse );
	self->iFader->BeginTransition();
	return 0;
	}

//////////////////////////////////////////////////////////////////////////
void CImageFadeControl::SetTransitionTime( TUint aTransitionTime )
	{
	iTransitionTime = aTransitionTime;
	}

